package cn.dankal.flyaudio.personal;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.callback.DKCallBackBoolean;
import cn.dankal.basiclib.pojo.CouponEntity;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.widget.CommonDialog;
import cn.dankal.basiclib.widget.swipetoloadlayout.SwipeToLoadLayout;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.CouponAdapter;
import cn.dankal.flyaudio.contract.MyCardContract;
import cn.dankal.flyaudio.friend.pickimage.StringUtil;
import cn.dankal.flyaudio.presenter.MyCardPresenter;

public class MyCardActivity extends BaseActivity implements MyCardContract.View {

    private RecyclerView recyclerView;

    private CouponAdapter adapter;

    private MyCardPresenter mPresenter;
    private MultipleStatusView statusView;

    private FrameLayout frameUsable, frameUsed, frameExpired, frameTransferred;

    private SmartRefreshLayout swipe_toload_layout;
    private TextView tvGetCoupin;
    private int page = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_card;
    }

    @Override
    protected void initComponents() {
        adapter = new CouponAdapter();
        mPresenter = new MyCardPresenter();
        mPresenter.attachView(this);
        swipe_toload_layout = findViewById(R.id.swipe_toload_layout);
        statusView = findViewById(R.id.multiple_status_view);
        tvGetCoupin = findViewById(R.id.tv_get_coupin);
        frameUsable = findViewById(R.id.frame_usable);
        frameUsed = findViewById(R.id.frame_used);
        frameExpired = findViewById(R.id.frame_expired);
        frameTransferred = findViewById(R.id.frame_transferred);
        swipe_toload_layout.setOnLoadMoreListener(new OnLoadMoreListener(){

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mPresenter.getDataList(page, "");
            }
        });
        swipe_toload_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page =1;
                adapter.clearData();
                mPresenter.getDataList(page, "");
            }
        });
        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        findViewById(R.id.iv_rule).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundle = new Bundle();
                        bundle.putString("type", "1");
                        ActivityUtils.startActivity(bundle, MyCardRuleActivity.class);
                    }
                }
        );
        tvGetCoupin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
            }
        });
        frameUsable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewAllFalse();
                setViewSelect(frameUsable, true);
                adapter.clearData();
                page=1;
                mPresenter.getDataList(page, "USABLE");
            }
        });

        frameUsed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewAllFalse();
                setViewSelect(frameUsed, true);
                adapter.clearData();
                page=1;
                mPresenter.getDataList(page, "USED");
            }
        });
        frameExpired.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewAllFalse();
                setViewSelect(frameExpired, true);
                adapter.clearData();
                page=1;
                mPresenter.getDataList(page, "EXPIRED");
            }
        });
        frameTransferred.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewAllFalse();
                setViewSelect(frameTransferred, true);
                adapter.clearData();
                page=1;
                mPresenter.getDataList(page, "TRANSFERRED");
            }
        });

        recyclerView = findViewById(R.id.swipe_target);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
        frameUsable.callOnClick();
    }

    private void setViewAllFalse() {
        setViewSelect(frameUsable, false);
        setViewSelect(frameUsed, false);
        setViewSelect(frameExpired, false);
        setViewSelect(frameTransferred, false);
    }

    private void setViewSelect(View view, boolean select) {
        view.setSelected(select);
    }

    @Override
    public void showProductList(List<CouponEntity> data) {
        swipe_toload_layout.finishRefresh();
        swipe_toload_layout.finishLoadMore();
        if(page==1) {
            if (data.isEmpty()) {
                statusView.showEmpty();
                return;
            }
            statusView.showContent();
            adapter.addDatas(data, true);
        }else{
            statusView.showContent();
            adapter.addDatas(data, false);
        }
    }

    @Override
    public void onError() {
        swipe_toload_layout.finishRefresh();
        swipe_toload_layout.finishLoadMore();
        statusView.showError();
    }

    @Override
    public void onExchangeSuccess(String id) {
        CommonDialog.Builder builder = new CommonDialog.Builder(this);

        builder.setTitle("领取卡劵");
        builder.setDes("您可在：我的卡包查看您的卡券");
        builder.setDesColor(getResources().getColor(R.color.color33));
//        builder.setDesColor(getResources().getColor(R.color.color00));

        builder.setNegativeButton("取消", getResources().getColor(R.color.color00), new DKCallBackBoolean() {
            @Override
            public void action(int type) {
                successDialog.dismiss();
            }
        });
        builder.setPositiveButton("查看", getResources().getColor(R.color.color_0085B4),
                new DKCallBackBoolean() {
                    @Override
                    public void action(int type) {
                        swipe_toload_layout.autoRefresh();
                        successDialog.dismiss();
                    }
                });

        successDialog = builder.create();
        successDialog.show();
    }

    @Override
    public void onExchangeError() {
        CommonDialog.Builder builder = new CommonDialog.Builder(this);
        builder.setSubtitle("券码错误");
        builder.setNegativeButton("取消", getResources().getColor(R.color.color00), new DKCallBackBoolean() {
            @Override
            public void action(int type) {
                errorDialog.dismiss();
            }
        });
        builder.setPositiveButton("重填", getResources().getColor(R.color.color_0085B4),
                new DKCallBackBoolean() {
                    @Override
                    public void action(int type) {
                        errorDialog.dismiss();
                        commonDialog.show();
                    }
                });

        errorDialog = builder.create();
        errorDialog.show();
    }

    private CommonDialog commonDialog;
    private CommonDialog errorDialog;
    private CommonDialog successDialog;

    private void showDialog() {
        CommonDialog.Builder builder = new CommonDialog.Builder(this);
        builder.setTitle("领取卡劵");
        builder.setDes("输入你收到的卡券领取码，点击“领取”即可获得一张新的卡券。");
        builder.setDesColor(getResources().getColor(R.color.color00));
        builder.setEditVisibility(true);

        builder.setNegativeButton("取消", getResources().getColor(R.color.color00), new DKCallBackBoolean() {
            @Override
            public void action(int type) {
                commonDialog.dismiss();
            }
        });
        builder.setPositiveButton("领取", getResources().getColor(R.color.color_0085B4),
                new DKCallBackBoolean() {
                    @Override
                    public void action(int type) {
                        EditText text = builder.getDialog().findViewById(R.id.edit_content);

                        if (!StringUtil.isEmpty(text.getText().toString())) {
                            commonDialog.dismiss();
                            mPresenter.getExchange(text.getText().toString());
                        } else {
                            ToastUtils.showShort("券码不能为空");
                        }


                    }
                });

        commonDialog = builder.create();
        commonDialog.show();
    }
}
